#include "stdafx.h"
#include "Timer.h"


CTimer::CTimer()
: m_TimeScale(1.0f)
{
	m_StartTime = CPreciseTime::now();
}

CTimer::~CTimer()
{
	
}

void CTimer::Update()
{
	CPreciseTime current_time = CPreciseTime::now() - m_StartTime;

	m_LastFrameTime = current_time - m_FrameStartTime;
	m_FrameStartTime = current_time;
	m_GameTime += m_LastFrameTime * m_TimeScale;

	m_fps.Update();
}

float CTimer::GetTime(ETimeType time_type) const
{
	switch (time_type)
	{
	case eTime_Instant:
		return CPreciseTime::now() - m_StartTime;

	case eTime_Game:
		return m_GameTime;

	case eTime_FrameStart:
		return m_FrameStartTime;

	default:
		assert(!"Invalid time type");
	}

	return 0;
}

float CTimer::GetFrameTime() const
{
	return m_LastFrameTime;
}

float CTimer::GetFPS(EFPSType fps_type) const
{
	switch(fps_type)
	{
	case eFPS_Instant:
		return m_fps.Instant();

	case eFPS_Max:
		return m_fps.Max();

	case eFPS_Min:
		return m_fps.Min();

	default:
		assert(!"Invalid fps type");
	}

	return 0;
}

void CTimer::SetTimeScale(float scale)
{
	m_TimeScale = scale;
}

SDateTime CTimer::GetDateTime() const
{
	//	get time
	time_t time_value;
	time(&time_value);

	//	convert to local time
	tm local_time;
	localtime_s(&local_time, &time_value);

	SDateTime dt;
	dt.year = local_time.tm_year + 1900;
	dt.month = local_time.tm_mon + 1;
	dt.day = local_time.tm_mday;
	dt.hours = local_time.tm_hour;
	dt.minutes = local_time.tm_min;
	dt.seconds = local_time.tm_sec;

	return dt;
}